using GameBaseFramework.Attribute;
using GameBaseFramework.Timer;
using GameLogic.AbilitySystem.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using GameBaseFramework.Extension;
using GameLogic.AbilitySystem.Tags;
using UnityEngine;

namespace GameLogic.AbilitySystem.Effect
{
    public enum EffectModifyType
    {
        /// <summary>
        /// 立即施加到AttributeSet中，只允许作用于Base值
        /// </summary>
        Instance,

        /// <summary>
        /// 在施加时立刻施加到AttributeSet的Current值中，且具有一定的持续时间，在持续时间结束后恢复到施加Effect之前的效果。此时只允许作用于Current值
        /// </summary>
        Lasting,

        /// <summary>
        /// 具有一定的持续时间，在持续时间内间隔固定的时间间隔就产生一次作用。只作用于Base值
        /// 目前只允许造成持续伤害
        /// </summary>
        Duration
    }

    /// <summary>
    /// 持续性Buff效果
    /// </summary>
    [Serializable]
    public class LastingEffectAttribute
    {
        public LastingEffectAttribute()
        {
            EffectAttributes = new();
            foreach (var i in Enum.GetValues(typeof(AttributeType)))
            {
                EffectAttributes.Add(new LinearFunctionModifier());
            }
        }
        [SerializeField]
        public List<LinearFunctionModifier> EffectAttributes; 
    }

    /// <summary>
    /// 生效一次的效果
    /// </summary>
    public class EffectAttribute
    {
        [SerializeField]
        public List<AttributeModifier> Modifiers;
    }

    /// <summary>
    /// 效果基类
    /// 分为持续、单次、持续多次三类
    /// </summary>
    [Serializable]
    [CreateAssetMenu(menuName = "数值系统/创建新的效果")]
    public class EffectBase : ScriptableObject
    {
        /// <summary>
        /// 标签集合
        /// </summary>
        public List<string> Tags;

        /// <summary>
        /// 有标签
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool ContainsTag(string tag)
        {
            return Tags.Contains(tag);
        }

        /// <summary>
        /// Effect施加类型，这决定了LastingEffectAttribute和EffectAttribute哪一个生效
        /// </summary>
        public EffectModifyType ModifyType = EffectModifyType.Instance;

        /// <summary>
        /// 持续数值变化情况
        /// </summary>
        public LastingEffectAttribute LastingEffectAttribute = new();

        /// <summary>
        /// 单次数值变化情况
        /// </summary>
        public EffectAttribute InstanceAttribute = new();

        /// <summary>
        /// （Type=Lasting/Duration时有用）Effect的持续时间
        /// </summary>
        public float LastingTime;

        /// <summary>
        /// （Type=Duration时有用）作用的间隔时间
        /// </summary>
        public float TimeDelta;
    }

    /// <summary>
    /// 活跃的Effect
    /// </summary>
    public class ActiveEffect
    {
        /// <summary>
        /// 指向的EffectBase
        /// </summary>
        public EffectBase effect;

        /// <summary>
        /// (Lasting/Duration)剩余的持续时间
        /// </summary>
        public float RemainingTime;

        /// <summary>
        /// (Duration)下一次生效的时间
        /// </summary>
        public float NextActivateTime;

        /// <summary>
        /// 乘数
        /// </summary>
        public float mul = 1.0f;
    }
}